//测试当前数据是否链接成功
import {ipcMain} from "electron";
import MySqlConnectInfo from "../../../src/model/mysql/MySqlConnectInfo";
import ResultDTO from "../../../src/model/ResultDTO";
import {Pool} from "mysql";
import DataSourceManager from "./index";

// @ts-ignore
ipcMain.handle('test-connect',    (event, connectInfoJson: string )=> {
    console.log("测试链接:", connectInfoJson)
    let connectInfo: MySqlConnectInfo = JSON.parse(connectInfoJson)
    return DataSourceManager.testConnection(connectInfo);
});


// @ts-ignore
ipcMain.handle('open-connect-and-return-databases',    (event, connectInfoJson: string )=> {
    let connectInfo: MySqlConnectInfo = JSON.parse(connectInfoJson)
    DataSourceManager.createPool(connectInfo);
    return DataSourceManager.getDataBaseList(<number>connectInfo.id)
});

ipcMain.handle('get-database-table-list',    (event, connectId: number, database: string )=> {
    return DataSourceManager.getDataBaseTableList(connectId, database)
});

ipcMain.handle('close-connect',    (event, connectId: number, database: string )=> {
    return DataSourceManager.closeAll(connectId)
});

ipcMain.handle('get_table_info', (event,connectId: number, database: string,tableName: string ) =>{
    let sql = `SELECT  table_name as table_name, table_rows as table_rows, 
        engine as engine, auto_increment as auto_increment, row_format as row_format, update_time as update_time, create_time as create_time, index_length as index_length, data_length as data_length, 
        table_collation as table_collation, table_comment as table_comment
         FROM  
            information_schema.TABLES 
         WHERE 
            TABLE_SCHEMA = '${database}' AND 
            TABLE_NAME = '${tableName}'`
    return DataSourceManager.queryDatabaseByConnectNameByOne(connectId, sql);

})
//获取数据表结构
ipcMain.handle('get_table_struct_info', (event,connectId: string, database: string,tableName: string ) =>{
    let sql = `show create table ${database}.${tableName}`
    return DataSourceManager.queryDatabaseByConnectNameByOne(Number(connectId), sql);
})
ipcMain.handle('get-mysql-collation-support', (event,connectId: string) =>{
    let sql = `SHOW COLLATION`
    return DataSourceManager.queryDataList(Number(connectId), sql);
})